package C;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class C40 {

    static class Work {
        String name;
        int watch;
        int start;
        int fork;
        int issue;
        int mr;
        int sum;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        int[] quan = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        ArrayList<Work> list = new ArrayList<>();
        for (int i=0;i<n;i++) {
            String[] s = sc.nextLine().split(" ");
            Work work = new Work();
            work.name = s[0].toLowerCase();
            for (int j=0;j<5;j++) {
                work.sum += Integer.parseInt(s[j+1])*quan[j];
            }
            list.add(work);
        }
        Collections.sort(list,(a,b)-> {
            return b.sum != a.sum ? b.sum - a.sum : a.name.compareTo(b.name);
        });
        for (Work work:list) {
            System.out.println(work.name);
        }
    }
}
